package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgContractDaoMapper;
import com.gg.user.gguser.dataobject.GgContractDataobject;
import com.gg.user.gguser.service.GgContractSerice;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class GgContractSericeImpl implements GgContractSerice {

    @Autowired
    private GgContractDaoMapper ggContractDaoMapper;

    /**
     * 增加
     */
    @Override
    public Object addContractItems(HttpServletResponse response, HttpServletRequest request, String ggCNumber, String ggCContractName, String ggCInstitutionalName, String ggCTypeOfContract, String ggCcontractAmount, String ggCpersonInCharge, String ggCAuditStatus, String ggCContractTime, String ggCDueTime, String ggCNatureOfContract, String ggCContractContact, String ggCTotalContractAmount, String ggCPaymentMethod, String ggCSignedBy, String ggCContractSeal, String ggCSignatureOfContract, String ggCRemarks, String ggCPploadInformation, String ggCExpirationReminder) throws Exception{
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggCUserId= JSONObject.parseObject(cookiceUser).getString("ggId");
            ggContractDaoMapper.addContractItems(ggCNumber, ggCContractName, ggCInstitutionalName, ggCTypeOfContract, ggCcontractAmount, ggCpersonInCharge, ggCAuditStatus, ggCContractTime, ggCDueTime, ggCNatureOfContract, ggCContractContact, ggCTotalContractAmount, ggCPaymentMethod, ggCSignedBy, ggCContractSeal, ggCSignatureOfContract, ggCRemarks, ggCPploadInformation, ggCExpirationReminder, ggCUserId);

            return return_format.return_format_All(0,"","");
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器报错！");
        }
    }

    /**
     * 删除
     */
    @Override
    public Object delectContract(HttpServletResponse response, HttpServletRequest request, String ggCNumber) throws Exception{
        try {
            // 查一下这个资源归不归他
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggCUserId= JSONObject.parseObject(cookiceUser).getString("ggId");

            return return_format.return_format_All(0,"","");
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器报错！");
        }
    }

    /**
     * 修改
     */
    @Override
    public Object updateContractItems(HttpServletResponse response, HttpServletRequest request, String ggCNumber, String ggCContractName, String ggCInstitutionalName, String ggCTypeOfContract, String ggCcontractAmount, String ggCpersonInCharge, String ggCAuditStatus, String ggCContractTime, String ggCDueTime, String ggCNatureOfContract, String ggCContractContact, String ggCTotalContractAmount, String ggCPaymentMethod, String ggCSignedBy, String ggCContractSeal, String ggCSignatureOfContract, String ggCRemarks, String ggCPploadInformation, String ggCExpirationReminder, Integer ggCId) throws Exception {
        // 查一下这个资源归不归他
        String cookiceUser = getHeaderUser.getHader(response,request,"gg");
        String ggCUserId= JSONObject.parseObject(cookiceUser).getString("ggId");
        GgContractDataobject ggContractDataobject = ggContractDaoMapper.getContractItems(ggCUserId, ggCId);
        if(ggContractDataobject != null){
            try {
                ggContractDaoMapper.updateContractItems(ggCNumber, ggCContractName, ggCInstitutionalName, ggCTypeOfContract, ggCcontractAmount, ggCpersonInCharge, ggCContractTime, ggCDueTime, ggCNatureOfContract, ggCContractContact, ggCTotalContractAmount, ggCPaymentMethod, ggCSignedBy, ggCSignatureOfContract, ggCRemarks, ggCPploadInformation, ggCExpirationReminder,   ggCContractSeal, ggCAuditStatus, ggCUserId, ggCId);
                return return_format.return_format_All(0,"","");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务器报错！");
            }

        }else {
            return return_format.return_format_error(100005,"不属于你的资源！");
        }
    }

    /**
     * 查询
     */
    @Override
    public Object getContract(HttpServletResponse response, HttpServletRequest request, Integer page, Integer pageNumber,String name, String type,String starttime,String endtime) throws Exception {
        if(page != null && page > 0 && pageNumber != null && pageNumber > 0 ){
            try {
                // 获取用户信息
                // 从cookice信息中过去用户信息 在这里只用要id就好
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 数据列表
                name = name!=null && name.equals("") ? null:name;
                type = type!=null && type.equals("") ? null:type;
                starttime = starttime!=null && starttime.equals("") ? null:starttime;
                endtime = endtime!=null && endtime.equals("") ? null:endtime;
                Integer ContractSelectCountNumber = ggContractDaoMapper.getContractSelectCount(ggIdUser, name, type, starttime, endtime);
                List<GgContractDataobject> ggContractSelectList = ggContractDaoMapper.getContractSelectList(ggIdUser,(page-1)*pageNumber, pageNumber, name, type, starttime, endtime);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(ContractSelectCountNumber/pageNumber),pageNumber,ContractSelectCountNumber,ggContractSelectList),"");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务器报错！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    public Object getContractItems(HttpServletResponse response, HttpServletRequest request, Integer itemsid) throws Exception{
        if(itemsid != null && itemsid > 0){
            // 获取用户信息
            // 从cookice信息中过去用户信息 在这里只用要id就好
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
            return return_format.return_format_All(0,ggContractDaoMapper.getContractItems(ggIdUser,itemsid),"");
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    public Object delectContractItmes(HttpServletResponse response, HttpServletRequest request, Integer itemsid) throws Exception {
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggCUserId= JSONObject.parseObject(cookiceUser).getString("ggId");
            ggContractDaoMapper.delectContract(itemsid,ggCUserId);
            return return_format.return_format_All(0,"","");
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器报错！");
        }
    }

    @Override
    public String uploadContract(HttpServletResponse response, HttpServletRequest request, MultipartFile file) throws Exception {
        String path="E:/" + System.currentTimeMillis() + file.getOriginalFilename();
        File newFile=new File(path);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newFile);
        return null;
    }
}
